//
//  JKIndexMyWelfareFriendsVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/24.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKIndexMyWelfareFriendsVC.h"

// view
#import "JKViewFriendsSegment.h"        //

// cell
#import "JKCellFriends.h"


// api
#import "ByApiMine.h"

// model
#import "ByModelFriendAndRelative.h"

@interface JKIndexMyWelfareFriendsVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) JKViewFriendsSegment *segment;

@property (nonatomic, assign) NSInteger type;
@property (nonatomic, assign) NSInteger page_index;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation JKIndexMyWelfareFriendsVC

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"亲朋好友";
    [[self.view viewWithTag:888] removeFromSuperview];
    
    [self addColorLeftBackBarButtonItem];
    
    [self createViews];
    
    _type = 1;
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
    
    [self fetchData];
}

#pragma mark - 请求数据

- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.type = [NSString stringWithFormat:@"%ld", _type];
    model.page_no = [NSString stringWithFormat:@"%ld",self.page_index];
    [ByApiMine fetchFriendList:model success:^(ByNetBaseResult *result) {
        NSInteger count = [result.data[@"count"] integerValue];
        NSMutableArray *temp = [NSMutableArray array];
        for (ByModelFriendAndRelative *model in result.data[@"list"]) {
            [temp addObject:model];
        }
        if (self.page_index == 1) {
            [self.dataArray removeAllObjects];
            [self.dataArray addObjectsFromArray:[temp copy]];
            [self.table.mj_footer resetNoMoreData];
        }else{
            [self.dataArray addObjectsFromArray:[temp copy]];
        }
        [self.table reloadData];
        [ByCoreToolAlert hideHud];
        if (self.dataArray.count == count) {
            [self.table.mj_header endRefreshing];
            [self.table.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
    }];
}


#pragma mark - 创建视图
- (void)createViews {
    
    _segment = [JKViewFriendsSegment new];
    _segment.segments = @[@"亲朋", @"好友"];
    [self.view addSubview:_segment];
    [_segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@60);
    }];
    __weak typeof(self) wself = self;
    _segment.selectBlock = ^(NSInteger index) {
        wself.type = index + 1;
        [wself.table.mj_header beginRefreshing];
    };
    
    [self createTableView];
}
- (void)createTableView {
    
    self.table = [[UITableView alloc] init];
    self.table.backgroundColor = TABLE_COLOR;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    [self.table mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(60, 0, 0, 0)).priorityMedium();
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        }
    }];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        _page_index = 1;
        [self fetchData];
    }];
    self.table.mj_header = header;
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        _page_index += 1;
        [self fetchData];
    }];
    self.table.mj_footer = footer;
    
    [self.table registerClass:[JKCellFriends class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JKCellFriends *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataArray[indexPath.section];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(JKCellFriends *cell) {
        cell.model = self.dataArray[indexPath.section];
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return UIView.new;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return UIView.new;
}

#pragma mark - getter
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
